package com.xin.data.simbapesa.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import com.xin.data.simbapesa.api.PU;
import com.xin.data.simbapesa.utils.LogUtil;


public class SimbaEditText extends AppCompatEditText implements TextWatcher, View.OnFocusChangeListener{


    private boolean isProgrammaticChange = false;


    private boolean isPaste = false;

    private boolean isEdit = false;


    private boolean afterPaste = false;

    private long startTime = 0;

    private long clickStartTime = 0;

    private long clickEndTime = 0;

    private String type;

    private String headStr = "";

    private String other;

    private boolean isSend;

    public SimbaEditText(Context context) {
        super(context);
        init();
    }

    public SimbaEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SimbaEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public String getHeadStr() {
        return headStr;
    }

    public void setHeadStr(String headStr) {
        this.headStr = headStr;
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        isProgrammaticChange = true;
        super.setText(text, type);
        isProgrammaticChange = false;
    }

    private void init() {
        setFocusable(true);
        setFocusableInTouchMode(true);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    public boolean isProgrammaticChange() {
        return !isProgrammaticChange;
    }


    @Override
    public boolean onTextContextMenuItem(int id) {
        if (id == android.R.id.paste) {
            isPaste = true;
            afterPaste = true;

            isEdit = false;
        }
        return super.onTextContextMenuItem(id);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        if (afterPaste) afterPaste = false;
        else isEdit = true;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) startTime = System.currentTimeMillis();
        else {
            long endTime = System.currentTimeMillis();
            String content = headStr + getText().toString();
            long time = endTime - startTime;
            StringBuilder pe = new StringBuilder();
            if (isPaste) pe.append("P");
            if (isEdit) pe.append("E");
            String remark;
            if (TextUtils.isEmpty(other)) {
                remark = content + "$$" + time + "$$" + pe;
            } else {
                remark = other + "$$" + content + "$$" + time + "$$" + pe;
            }
            LogUtil.e("me",type+"--onFocusChange--"+remark);
            if (!TextUtils.isEmpty(type)) {
                isSend=true;
                PU.userBuriedPoint(type, remark);
            }
        }
    }

    public void setType(String type) {
        this.type = type;
    }

    public long getClickStartTime() {
        return clickStartTime;
    }

    public void setClickStartTime(long clickStartTime) {
        this.clickStartTime = clickStartTime;
    }

    public long getClickEndTime() {
        return clickEndTime;
    }

    public void setClickEndTime(long clickEndTime) {
        this.clickEndTime = clickEndTime;
    }

    public void sendEvent(String content) {
        long time = clickEndTime - clickStartTime;
        String remark = content + "$$" + time;
        LogUtil.e("me", type + "--sendEvent--" + remark);
        if (!TextUtils.isEmpty(type)) {
            isSend=true;
            PU.userBuriedPoint(type, remark);
        }
    }

    public String getOther() {
        return other;
    }

    public void setOther(String other) {
        this.other = other;
    }

    public boolean isSend() {
        return isSend;
    }

    public void setSend(boolean send) {
        isSend = send;
    }

}
